﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

namespace CssDef
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "css(*.css)|*.css|txt(*.txt)|*.txt|js(*.js)|*.js";

            if ( openFileDialog1.ShowDialog() == DialogResult.OK )
            {
                string path = openFileDialog1.FileName;
                try 
                {
                    using (StreamReader sr = new StreamReader(path)) 
                    {
                        Boolean bResult = true;
                        String line;
                        while ((line = sr.ReadLine()) != null) 
                        {
                            bResult = IsPureCss(line);
                            if (bResult == false)
                            {
                                break;
                            }
                        }

                        if (bResult == false)
                        {
                            MessageBox.Show("Not CSS!");
                        }
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }else{
                MessageBox.Show("请选择文件！");
            }
        }

        public string ReplaceComment(string strCss)
        {
            int i = strCss.IndexOf("/*");
            int j = strCss.IndexOf("*/");
            int nLength = j - i;
            string strRet = strCss.Remove(i, nLength);
            return strRet;
        }

        public Boolean IsComment(string strCss)
        {
            int i = strCss.IndexOf("/*");
            if (i != -1)
            {
                int j = strCss.IndexOf("*/");
                if (j != -1)
                {
                    return true;
                }
            }

            return false;
        }

        public Boolean IsPureCss(string strCss)
        {
            Boolean bResult = false;

            int i,j;

            while(IsComment(strCss))
            {
                strCss = ReplaceComment(strCss);
            }

            i = strCss.IndexOf("<");
            j = strCss.IndexOf("%3c");
            if ((i == -1) && (j == -1))
            {
                i = strCss.IndexOf(">");
                j = strCss.IndexOf("%3e");
                if ((i == -1) && (j == -1))
                {
                    i = strCss.IndexOf("expression");
                    if (i == -1)
                    {
                        i = strCss.IndexOf("&#x");
                        if (i == -1)
                        {
                            i = strCss.IndexOf("\\");
                            if (i == -1)
                            {
                                i = strCss.IndexOf("import");
                                if (i == -1)
                                {
                                    i = strCss.IndexOf("htm");
                                    if (i == -1)
                                    {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return bResult;
        }
    }
}
